package com.eduhzy.ttw.teacher.mvp.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.SPUtils;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.core.EventBusTags;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.commonsdk.entity.ClassDetailsData;
import com.eduhzy.ttw.commonsdk.entity.MineClassData;
import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.teacher.R;
import com.eduhzy.ttw.teacher.di.component.DaggerClassDetailsComponent;
import com.eduhzy.ttw.teacher.mvp.contract.ClassDetailsContract;
import com.eduhzy.ttw.teacher.mvp.presenter.ClassDetailsPresenter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.qmuiteam.qmui.widget.QMUIRadiusImageView;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.zhy.autolayout.AutoLinearLayout;

import org.simple.eventbus.Subscriber;

import butterknife.BindView;
import butterknife.OnClick;

import static com.jess.arms.utils.Preconditions.checkNotNull;

;

/**
 * 班级详情
 *
 * @author xubo
 */
@Route(path = RouterHub.APP_CLASSDETAILSACTIVITY)
public class ClassDetailsActivity extends BaseActivity<ClassDetailsPresenter> implements ClassDetailsContract.View {
    @Autowired(name = Constants.ROUTER_DATA)
    MineClassData mineClassData;
    @Autowired(name = Constants.ROUTER_BOOLEAN)
    boolean isJoin;
    @Autowired(name = Constants.ROUTER_STRING)
    String joinId;


    @BindView(R.id.iv_avatar)
    QMUIRadiusImageView ivAvatar;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.btn_exit)
    Button btnExit;
    @BindView(R.id.tv_sid)
    TextView tvSid;
    @BindView(R.id.tv_grade)
    TextView tvGrade;
    @BindView(R.id.tv_creator)
    TextView tvCreator;
    @BindView(R.id.tv_teacher)
    TextView tvTeacher;
    @BindView(R.id.tv_student)
    TextView tvStudent;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout mSwipeLayout;
    @BindView(R.id.content)
    View content;
    @BindView(R.id.layout_grade)
    AutoLinearLayout layoutGrade;
    @BindView(R.id.layout_teacher)
    AutoLinearLayout layoutTeacher;
    @BindView(R.id.layout_transfer)
    AutoLinearLayout layoutTransfer;
    private ClassDetailsData mData;
    private QMUITipDialog dialog;


    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        //如找不到该类,请编译一下项目
        DaggerClassDetailsComponent
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
        return R.layout.activity_class_details;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        request();
        mSwipeLayout.setOnRefreshListener(() -> request());
    }

    private void request() {
        if (isJoin) {
            mPresenter.getTargetByQrCode(joinId, SPUtils.getInstance().getString(Constants.USERID));
        } else {
            if (ObjectUtils.isNotEmpty(mineClassData)) {
                mPresenter.getClassInfoById(mineClassData.getId());
            }
        }
    }

    @Override
    public void showLoading() {
        if (mSwipeLayout != null) {
            mSwipeLayout.setRefreshing(true);
        }
    }

    @Override
    public void hideLoading() {
        if (mSwipeLayout != null) {
            mSwipeLayout.setRefreshing(false);
        }
    }


    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        RxUtil.showTipDialog(this, QMUITipDialog.Builder.ICON_TYPE_NOTHING, message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public void update(ClassDetailsData data) {
        if (isJoin && !data.isInClass()) {
            btnExit.setText("加入班级");
        } else {
            btnExit.setText("退出");
        }
        this.mData = data;
        tvName.setText(data.getBjmc());
        tvSid.setText(data.getBaseNo());
        tvGrade.setText(data.getNjmc());
        tvCreator.setText(data.getBzrName());
        if (data.getBzrgh() != null && data.getBzrgh().equals(SPUtils.getInstance().getString(Constants.USERID))) {
            layoutTransfer.setVisibility(View.VISIBLE);
        } else {
            layoutTransfer.setVisibility(View.GONE);
        }
        tvTeacher.setText(data.getTeaCount() + "人");
        tvStudent.setText(data.getStuCount() + "人");

        content.setVisibility(View.VISIBLE);

    }

    @Subscriber(tag = RouterHub.APP_CLASSDETAILSACTIVITY)
    public void onEvents(Message msg) {
        switch (msg.what) {
            case EventBusTags.TRANSFER_CLASS_SUCCESS:
                killMyself();
                break;
            case EventBusTags.DELETE_STUDENT_SUCCESS:
                mData.setStuCount(String.valueOf(Integer.parseInt(mData.getTeaCount()) - 1));
                tvStudent.setText(mData.getStuCount() + "人");
                break;
            case EventBusTags.UPDATE_CLASS_SUCCESS:
                mData.setBjmc((String) msg.obj);
                tvName.setText(mData.getBjmc());
                break;
            default:
        }
    }

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    public void showTipLoading(boolean b) {
        if (dialog == null) {
            dialog = RxUtil.showLoading(this, "请稍后···");
        }
        if (b) {
            dialog.show();
        } else {
            dialog.dismiss();
        }
    }

    @OnClick({R.id.iv_qr_code, R.id.layout_grade,
            R.id.layout_teacher, R.id.layout_student,
            R.id.btn_exit, R.id.layout_transfer,
            R.id.layout_class})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_qr_code:
                ARouter.getInstance().build(RouterHub.APP_GENERATECODEACTIVITY)
                        .withString(Intent.EXTRA_TITLE, Constants.CLASS_QRCODE)
                        .withString(Constants.GENERATE_CODE, RxUtil.getClassCode(mData.getBaseNo()))
                        .withInt(Constants.QR_CODE_TYPE, Constants.QRCODE.Clazz)
                        .navigation();
                break;
            case R.id.layout_grade:
                break;
            case R.id.layout_class:
                ARouter.getInstance().build(RouterHub.APP_UPDATECLASSACTIVITY)
                        .withString(Intent.EXTRA_TITLE, Constants.UPDATE_CLASS)
                        .withParcelable(Constants.ROUTER_DATA, mData)
                        .navigation(getActivity());
                break;
            case R.id.layout_transfer:
                if (Integer.parseInt(mData.getTeaCount()) <= 1) {
                    showMessage("当前无教师可转让");
                    request();
                    return;
                } else {
                    ARouter.getInstance().build(RouterHub.APP_CLASSTRANSFERACTIVITY)
                            .withString(Intent.EXTRA_TITLE, Constants.TRANSFER_CLASS)
                            .withParcelable(Constants.ROUTER_DATA, mData)
                            .navigation(getActivity());
                }
                break;
            case R.id.layout_teacher:
                ARouter.getInstance().build(RouterHub.APP_CLASSTEACHERACTIVITY)
                        .withString(Intent.EXTRA_TITLE, Constants.CLASST_TEACHER)
                        .withParcelable(Constants.ROUTER_DATA, mData)
                        .navigation();
                break;
            case R.id.layout_student:
                ARouter.getInstance().build(RouterHub.APP_CLASSSTUDENTACTIVITY)
                        .withString(Intent.EXTRA_TITLE, Constants.CLASST_STUDENT)
                        .withParcelable(Constants.ROUTER_DATA, mData)
                        .navigation();
                break;
            case R.id.btn_exit:
                if (isJoin && !mData.isInClass()) {
                    mPresenter.applyClass(mData.getId(), SPUtils.getInstance().getString(Constants.USERID));
                } else {
                    mPresenter.dropClass(mData.getId(), SPUtils.getInstance().getString(Constants.USERID));
                }
                break;
            default:
        }
    }

}
